import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:videotop/base_model.dart';
import 'package:videotop/jsonbean/login_bean.dart';
import 'package:videotop/myinfo.dart';
import 'package:videotop/widget/choosesex.dart';
import 'package:videotop/widget/home.dart';
import 'package:videotop/widget/regemail.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';

import '../util.dart';

/// created by hx
///    2021/1/23:16:22

class LoginRegModel extends BaseModel{

  Util util;
  MyInfo myInfo;
  BuildContext context;
  LoginRegModel(this.context, this.util,this.myInfo);

  String appleId = '';

  void applelogIn() async {
    final credential = await SignInWithApple.getAppleIDCredential(
      scopes: [
        AppleIDAuthorizationScopes.email,
        AppleIDAuthorizationScopes.fullName,
      ],
    );
    appleId = credential.userIdentifier;
    notifyListeners();
    getApplelogin();
  }


  getApplelogin() async {
    util.waiting(context);
    LoginBean loginBean = await util.login(3, appleId,'');
    Navigator.pop(context);
    if (loginBean.status == 1) {
      User user = loginBean.user;
      util.spfs.setInt('uid', user.uid);
      util.spfs.setInt('type', 3);
      util.spfs.setString('logid', appleId);
      util.spfs.setString('psd', '');
      myInfo.setMyInfo(user);
      Navigator.pushAndRemoveUntil(
        context,
        new MaterialPageRoute(builder: (context) => new Home()),
            (route) => route == null,
      );
    } else {
      Navigator.push(
          context,
          new MaterialPageRoute(
              builder: (context) => new ChooseSex(
                type: 3,
                logid: appleId,
              )));
    }
  }

  goEmail(){
    Navigator.push(
        context,
        new MaterialPageRoute(
            builder: (context) => new RegEmail()));
  }
}